package com.android.clinic.doctors.app.ui.widget;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.support.v4.app.ActivityCompat;
import android.view.View;

import com.android.clinic.doctors.app.ui.widget.dialog.CustomerAlertDialog;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by cheng on 2018/6/26.
 */
public class UIAlertView {

    private static CustomerAlertDialog dialog = null;
    private static OnThemeDialogListener listener;

    private static void setOnDismissUpdateListener(OnThemeDialogListener onThemeDialogListener) {
        listener = onThemeDialogListener;
    }

    public interface OnThemeDialogListener {
        void OnDismissButton();

        void OnSureButton();
    }

    /**
     * 拨打电话
     *
     * @param context
     * @param title
     * @param phoneNum
     */
    public static void callPhoneDialog(final Context context, String title, final String phoneNum) {
        dialog = new CustomerAlertDialog(context);
        dialog.setTitle(title);
        dialog.setContent(phoneNum);
        dialog.setOnClickListener(new CustomerAlertDialog.OnClickButtonListener() {
            @Override
            public void onClick(View v, CustomerAlertDialog.ClickType type) {
                if (type == CustomerAlertDialog.ClickType.CONFIRM) {
                    if (ActivityCompat.checkSelfPermission(context, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                        return;
                    }
                    Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phoneNum));
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(intent);
                }
            }
        });
        dialog.show();
    }

    /**
     * 弹出框
     *
     * @param context
     */
    public static void alertDialog(final Context context, final String title, final String content, final OnThemeDialogListener onDismissUpdateListener, String submitName, String cancelName) {
        setOnDismissUpdateListener(onDismissUpdateListener);
        dialog = new CustomerAlertDialog(context);
        dialog.setCancelable(false);
        dialog.setTitle(title);
        dialog.setContent(content);
        dialog.setSubmitName(submitName);
        dialog.setCancelName(cancelName);
        dialog.setOnClickListener((v, type) -> {
            if (onDismissUpdateListener != null) {
                if (type == CustomerAlertDialog.ClickType.CONFIRM) {
                    onDismissUpdateListener.OnSureButton();
                } else if (type == CustomerAlertDialog.ClickType.CANCEL) {
                    onDismissUpdateListener.OnDismissButton();
                }
            }

        });
        dialog.show();
    }


    /**
     * 打开百度地图
     *
     * @param context
     * @param city
     * @param title
     * @param address
     * @param lat
     * @param lon
     */
    @SuppressWarnings("deprecation")
    public static void openMap(final Context context, String city, String title, String address, String lat,
                               String lon) {
        if (isAvailable(context, "com.baidu.BaiduMap")) {// 安装百度地图
            try {
                // 根据经纬度
                Intent intent = new Intent();
                intent.setData(Uri.parse("baidumap://map/geocoder?location=" + lat + "," + lon));
                context.startActivity(intent);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else if (isAvailable(context, "com.autonavi.minimap")) {// 安装高德地图
            try {
                Intent intent = new Intent();
                intent.setData(Uri.parse("androidamap://viewMap?sourceApplication=" + city + "&poiname="
                        + address + "&lat=" + lat + "&lon=" + lon + "&dev=0"));
                context.startActivity(intent);
            } catch (Exception e) {
                e.printStackTrace();
            }

        } else {
            try {
                Uri uri = Uri.parse("http://api.map.baidu.com/geocoder?location=" + lat + "," +
                        lon + "&coord_type=gcj02&output=html&src=爱帮诊");
                Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                context.startActivity(intent);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

    /**
     * 检查手机上是否安装了指定的软件
     *
     * @param context
     * @param packageName ：应用包名
     * @return
     */
    public static boolean isAvailable(Context context, String packageName) {
        // PackageManager
        final PackageManager packageManager = context.getPackageManager();
        // 获取所有已安装程序的包信息
        List<PackageInfo> packageInfo = packageManager.getInstalledPackages(0);
        // 用于存储所有已安装程序的包名
        List<String> packageNames = new ArrayList<String>();
        // 从packageInfo中将包名字逐一取出，压入pName list中
        if (packageInfo != null) {
            for (int i = 0; i < packageInfo.size(); i++) {
                String packName = packageInfo.get(i).packageName;
                packageNames.add(packName);
            }
        }
        // 判断packageNames中是否有目标程序的包名，有TRUE，没有FALSE
        return packageNames.contains(packageName);
    }

}
